package com.dsg.controller.account;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountCompare;
import com.dsg.service.account.IDsgAccountCompareService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 天鑽公司与银行账比较 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/account/dsgAccountCompare")
public class DsgAccountCompareController extends BaseController {

    @Autowired
    private IDsgAccountCompareService service;

    /**
     * 查询列表（分页）
     *
     * @param dsgAccountCompare
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgAccountCompare dsgAccountCompare) {
        // 分頁
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("CREATE_TIME");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
//        dsgAccountCompare.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = service.selectByList(page, dsgAccountCompare);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 查询总账汇总
     *
     * @param accountNo
     * @return
     */
    @GetMapping(value = "/getDsgAccountCompare")
    @ApiOperation("查询总账汇总")
    public AjaxResult getDsgAccountCompare(String accountNo) {
        DsgAccountCompare dac = service.getSumDsgAccountCompare(accountNo, SingletonLoginUtils.getSystemEntCode());
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dac);
        return ajaxResult;
    }

    /**
     * 更新checkBank數據
     *
     * @param dsgAccountCompare
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/checkBank")
    @ApiOperation("更新checkBank數據")
    public AjaxResult checkBank(@RequestBody DsgAccountCompare dsgAccountCompare) {
        dsgAccountCompare = service.checkBank(dsgAccountCompare);
        if (dsgAccountCompare != null) {
            return json(MessageConstants.SSO_STATUS_SUCCESS,dsgAccountCompare);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
